package com.permgmt.controller;

import com.permgmt.entity.StockTransactionRecord;
import com.permgmt.entity.model.ResultFormat;
import com.permgmt.service.IStockTransactionRecordService;
import com.permgmt.utils.ResultUtil;
import com.permgmt.utils.StockUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 前端控制器
 *
 * @author LIAO
 * @since 2020-06-24
 */
@RestController
@RequestMapping("/stock-tr")
public class StockTransactionRecordController {

    @Autowired
    private IStockTransactionRecordService service;

    /**
     * 查询列表
     */
    @GetMapping("statistics-profit")
    @CrossOrigin
    public ResultFormat statisticsProfit() {
	return ResultUtil.success(service.statisticsProfit());
    }

    /**
     * 查询列表
     */
    @GetMapping("list")
    @CrossOrigin
    public ResultFormat list() {
	return ResultUtil.success(service.pageList());
    }

    /**
     * 添加
     */
    @PostMapping("save")
    @CrossOrigin
    public ResultFormat save(@RequestBody StockTransactionRecord record) {

	record.setCost(StockUtil.cost(record.getPric() * record.getTotalNum(), record.getOperation()));
	record.setSurplusNum(record.getTotalNum());

	return ResultUtil.result(service.save(record));
    }

    /**
     * 修改
     */
    @PostMapping("edit")
    @CrossOrigin
    public ResultFormat edit(@RequestBody StockTransactionRecord record) {
	return ResultUtil.result(service.updateById(record));
    }

    /**
     * 卖掉
     */
    @PostMapping("sell")
    @CrossOrigin
    public ResultFormat sell(@RequestBody StockTransactionRecord record) {

	return ResultUtil.result(service.sell(record));
    }

    /**
     * 删除
     */
    @PostMapping("del")
    @CrossOrigin
    public ResultFormat del(String id) {

	return ResultUtil.result(service.removeById(id));
    }
}
